package com.cskaoyan.controller;


import com.cskaoyan.bean.bo.order.OrderBo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.OrderInfoVo;
import com.cskaoyan.bean.vo.back.configmanagement.ChannelVo;
import com.cskaoyan.bean.vo.ordervo.OrderUserVo;
import com.cskaoyan.service.OrderGoodsService;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.back.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName:OrderController
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/12 09:28
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 订单查询
     *
     * @return
     */
    @RequiresPermissions("admin:order:list")
    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam, OrderBo orderBo) {

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketOrder> list = orderService.list(orderBo);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(list);
        CommonData data = CommonData.data(pageInfo);
        return BaseRespVo.ok(data);
    }

    @Autowired
    UserService userService;
    @Autowired
    OrderGoodsService orderGoodsService;

    /**
     * 查看订单详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:order:read")
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {

        List<MarketOrderGoods> orderGoods = orderGoodsService.orderDetail(id);
        MarketOrder order = orderService.orderDetail(id);
        OrderUserVo user = userService.orderDetail(order.getUserId());
        OrderInfoVo orderInfoVo = new OrderInfoVo(orderGoods, user, order);
        return BaseRespVo.ok(orderInfoVo);
    }
    @RequiresPermissions("admin:order:delete")
    @RequestMapping("delete")
    public BaseRespVo delete() {
        return BaseRespVo.invalidParameter("订单不能删除");
    }

    /**
     * 发货
     *
     * @param map
     * @return
     */
    @RequiresPermissions("admin:order:ship")
    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody Map map) {

        orderService.ship(map);
        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:order:refund")
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        orderService.refund(map);
        return BaseRespVo.ok();
    }

    /**
     * 快递选项
     *
     * @return
     */
    @RequestMapping("channel")
    public ChannelVo channel() {
        List<MarketChannel> channel = orderService.channel();
        return ChannelVo.ok(channel);
    }

}
